package com.dave.gd.core.common;

import static org.apache.ibatis.jdbc.SqlBuilder.AND;
import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.VALUES;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

import java.util.Map;
import java.util.Map.Entry;

import com.dave.gd.common.util.ClassInfoUtils;
import com.dave.gd.common.util.Constants;
import com.dave.gd.core.base.entity.impl.AbstractBaseEntity;
import com.dave.gd.core.base.entity.impl.BaseEntityMeta;
import com.dave.gd.core.util.SqlDealUtil;

public class CUDTemplate<T extends AbstractBaseEntity> {
    public String insert(T obj) {
        Class clazz = obj.getClass();
        BEGIN();
        
        INSERT_INTO(ClassInfoUtils.getTableName(clazz));
        //        ClassInfoUtils.caculationColumnList(clazz);
        VALUES(ClassInfoUtils.returnInsertColumnsName(obj, clazz),
            ClassInfoUtils.returnInsertColumnsDefine(obj, clazz));
        
        return SQL();
    }
    
    public String update(T obj) {
        Class clazz = obj.getClass();
        BEGIN();
        
        UPDATE(ClassInfoUtils.getTableName(clazz));
        //        ClassInfoUtils.caculationColumnList(clazz);
        SET(ClassInfoUtils.returnUpdateSet(clazz));
        WHERE(ClassInfoUtils.getIdField(clazz) + "=#{" + ClassInfoUtils.getIdFieldName(clazz) + "}");
        
        return SQL();
    }
    
    public String delete(T obj) {
        Class clazz = obj.getClass();
        BEGIN();
        
        DELETE_FROM(ClassInfoUtils.getTableName(clazz));
        //        ClassInfoUtils.caculationColumnList(clazz);
        WHERE(ClassInfoUtils.getIdField(clazz) + "=#{" + ClassInfoUtils.getIdFieldName(clazz) + "}");
        
        return SQL();
    }
    
    public String getObjectByMap(Map<String, Object> map) {
        //        BaseEntityMeta obj = (BaseEntityMeta) map.get(Constants.POJO_KEY);
        Class clazz = (Class) map.get(Constants.POJO_KEY);
        BEGIN();
        SELECT(" " + SqlDealUtil.createColumn(ClassInfoUtils.getTableName(clazz), ""));
        FROM(ClassInfoUtils.getTableName(clazz));
        WHERE(" 1=1 ");
        
        for (Entry<String, Object> entry : map.entrySet()) {
            if (entry.getValue() instanceof BaseEntityMeta) {
                continue;
            }
            if (entry.getValue() instanceof Class) {
                continue;
            }
            AND();
            WHERE(entry.getKey() + "=#{" + entry.getKey() + "}");
            
        }
        
        return SQL();
    }
    
}
